﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using CarRent.BL;
using CarRent.Pages;
using CarRent.BL.Models;

namespace CarRent
{
    public partial class Booking : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ltrRequestBy.Text = User.Identity.Name;

                //System.Security.Principal.WindowsPrincipal r = (System.Security.Principal.WindowsPrincipal)User;
                //IsAdminRole();
                UserImpl userImpl = new UserImpl();
                ddlRequestFor.DataSource = userImpl.GetAll();
                ddlRequestFor.DataValueField = "UserID";
                ddlRequestFor.DataTextField = "UserName";
                ddlRequestFor.DataBind();

                //CarImpl carImpl = new CarImpl();
                //ddlCar.DataSource = carImpl.GetAll();
                //ddlCar.DataValueField = "CarID";
                //ddlCar.DataTextField = "CarName";
                //ddlCar.DataBind();
            }
        }

        public bool IsAdminRole()
        {
            AppDomain domain = System.Threading.Thread.GetDomain();

            domain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
            System.Security.Principal.WindowsPrincipal principle = (System.Security.Principal.WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
            return principle.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            DateTime? fromDate, toDate;
            if (this.fromDate.Text == string.Empty)
            {
                fromDate = null;
            }
            else
            {
                fromDate = DateTime.Parse(this.fromDate.Text + ' ' + this.fromTime.Text);
            }

            if (this.toDate.Text == string.Empty)
            {
                toDate = null;
            }
            else
            {
                toDate = DateTime.Parse(this.toDate.Text + ' ' + this.toTime.Text);
            }


            BookingImpl impl = new BookingImpl();
            BookingInfo bookingInfo = new BookingInfo
            {
                RequestBy = ltrRequestBy.Text,
                RequestFor = int.Parse(ddlRequestFor.SelectedValue),
                FromDateTime = fromDate,
                ToDateTime = toDate,
                Destination = this.txtDestination.Text,
                Purpose = this.txtPurpose.Text
                //CarID = int.Parse(this.ddlCar.SelectedValue)
            };
            if (impl.InsertBooking(bookingInfo))
            {
                litStatus.Text = "Register the new booking successful.";
                pnlStatus.ForeColor = System.Drawing.Color.Blue;

                this.fromDate.Text = this.fromTime.Text = this.toDate.Text = this.toTime.Text = this.txtDestination.Text = this.txtPurpose.Text = string.Empty;
            }
            else
            {
                litStatus.Text = "Register the new booking failed.";
                pnlStatus.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}